#include "string_stack.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
int MAX = -1;
char appendbuf[] = {'1', '\0'};
static int privatePeek(char *stack, char *c);
int initMax(int max) { return MAX = max; }
/* store c at the end(top) of stack */
bool push(char *stack, char c) {
  if (strlen(stack) >= MAX)
    return false;
  *appendbuf = c;
  strcat(stack, appendbuf);
  return true;
}
bool pop(char *stack, char *c) {
  int index = privatePeek(stack, c);
  if (index < 0)
    return false;
  char *last = stack + index;
  *last = 0;
  return true;
}
bool peek(char *stack, char *c) { return privatePeek(stack, c) >= 0; }
static inline int privatePeek(char *stack, char *c) {
  if (!(*stack))
    return -1;
  char *last = stack + strlen(stack) - 1;
  *c = *last;
  return last - stack;
}